﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace Formality
{
    public sealed class Pair
    {
        public static Pair<T1, T2> New<T1, T2>(T1 t1, T2 t2)
        {
            return new Pair<T1, T2>(t1, t2);
        }
    }

    public class Pair<T1, T2>
    {
        public Pair() { }
        public Pair(T1 t1, T2 t2) { First = t1; Second = t2; }

        public T1 First { get; set; }
        public T2 Second { get; set; }

        public override bool Equals(object obj)
        {
            var p = obj as Pair<T1, T2>;

            return p != null
                && Check(First, p.First)
                && Check(Second, p.Second);
        }

        public override int GetHashCode()
        {
            var Hash = 0;

            if (First != null)
                Hash = First.GetHashCode();
            if (Second != null)
                Hash ^= Second.GetHashCode();

            return Hash;
        }

        public override string ToString()
        {
            return string.Format("Pair{{{0}, {1}}}", Dump(First), Dump(Second));
        }

        private bool Check<T>(T a, T b)
        {
            return a == null || b == null ? a == null && b == null : a.Equals(b);
        }

        private string Dump<T>(T t)
        {
            return t == null ? "null" : t.ToString();
        }
    }
}
